<?php

class DocumentDAO extends DAO{


	public function getById($id){
		try
		{
			$id =Validator::escapeSql($id);
			$query="select * from DOCUMENTS where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$docInfoDAO = $this->factory->getDocumentInfoDAO();
			$typDocDAO = $this->factory->getTypeDocumentDAO();
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new Document((int)$Bean->ID);
				$thebean->setDefault((int)$Bean->IS_DEFAULT);
				$thebean->setParentCategorieId((int)$Bean->PARENT_CAT_ID);
				$thebean->setUrl($Bean->URL);
				$thebean->setInfos($docInfoDAO->getAllByDocument($thebean));
				$thebean->setTypeDocument($typDocDAO->getById((int)$Bean->TYPE_DOCUMENT));
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function remove($Bean){
		try{
			$docInfDAO = $this->factory->getDocumentInfoDAO();
			foreach ($Bean->getInfos() as $Inf){
				$docInfDAO->remove($Inf);
			}
			$query="delete from DOCUMENTS where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			
			$infoDAO = $this->factory->getDocumentInfoDAO();
			foreach ($Bean->getInfos() as $Info){
				$infoDAO->update($Info);
			}
			
			$query = "update DOCUMENTS set TYPE_DOCUMENT=".Validator::escapeSql($Bean->getTypeDocument()->getId())." ,parent_cat_id=".Validator::escapeSql($Bean->getParentCategorieId())." ,url='".Validator::escapeSql($Bean->getUrl())."' ,is_default=".Validator::escapeSql($Bean->isDefault())." where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

		}
		return $bool;
	}
	function add($Bean){
		try{
			if (is_null($Bean->getId())){
				
				$infoDAO = $this->factory->getDocumentInfoDAO();
				foreach ($Bean->getInfos() as $Info){
					$infoDAO->add($Info);
				}
				$thebean = null;
				
				$insertQuery = "insert into DOCUMENTS (url,is_default,parent_cat_id,type_document) values ('".Validator::escapeSql($Bean->getUrl())."','".Validator::escapeSql($Bean->isDefault())."',".Validator::escapeSql($Bean->getParentCategorieId()).",".Validator::escapeSql($Bean->getTypeDocument()->getId()).");";
				$this->pdo->exec($insertQuery);
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}
		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}
	function getAllByCategorie($Categorie){
		try
		{
			if(is_a($Categorie, 'Categorie')){
				$BeanArray = array();
				$query="select * from DOCUMENTS where parent_cat_id=".Validator::escapeSql($Categorie->getId()).";";
				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				$thebean = null;
				foreach($array_obj as $Bean){
					$thebean = $this->getById((int)$Bean->ID);
					array_push($BeanArray,$thebean);
				}
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
	function getDefaultByCategorie($Categorie){
		try
		{
			$thebean = null;
			if(is_a($Categorie, 'Categorie')){
				$BeanArray = array();
				$query="select * from DOCUMENTS where parent_cat_id=".Validator::escapeSql($Categorie->getId())." and is_default=1;";

				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);

				foreach($array_obj as $Bean){

					$thebean = $this->getById((int)$Bean->ID);

				}

			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;

	}
}
?>